import axios from 'axios'

const API_BASE_URL = 'http://10.1.6.130/api/demo' // 使用代理路径解决跨域问题
axios.defaults.withCredentials = true;
export interface User {
    id?: number
    username: string
    pwd: string
    age: number
    realName: string
    email: string
    stuNum: string
    className: number
}

interface Movie {
    id?: number
    title: string
    director: string
    year: number
    description: string
    userId: number
}

interface Comment {
    id?: number
    content: string
    movieId: number
    userId: number
}

// 用户注册
// export const registerUser = async (user: User) => {
//     return axios.post(`${API_BASE_URL}/reg`, user)
// }
export const registerUser = async (user: User) => {
    return axios.post(`${API_BASE_URL}/reg`, {
        ...user,
        typeIndex: 0 // 对应 users 表
    })
  }
// 用户登录
// export const loginUser = async (credentials: { username: string; pwd: string }) => {
//     return axios.post(`${API_BASE_URL}/login`, credentials)
// }
// 用户登录
export const loginUser = async (credentials: { username: string; pwd: string }) => {
    return axios.post(`${API_BASE_URL}/login`, {
        ...credentials,
        typeIndex: 0 // 对应 users 表
    })
  }
// 获取所有用户
export const getAllUsers = async () => {
    return axios.get(`${API_BASE_URL}/users`)
}

// 添加电影
export const addMovie = async (movie: Movie) => {
    return axios.post(`${API_BASE_URL}/additem`, { ...movie, typeIndex: 3 })
}

// 获取所有电影
export const getAllMovies = async () => {
    return axios.get(`${API_BASE_URL}/getallitems`, { params: { typeIndex: 3 } })
}

// 获取单个电影
export const getMovieById = async (id: number) => {
    return axios.get(`${API_BASE_URL}/getsingle`, { params: { id, typeIndex: 3 } })
}

// 添加评论
export const addComment = async (comment: Comment) => {
    return axios.post(`${API_BASE_URL}/additem`, { ...comment, typeIndex: 4 })
}

// 获取电影评论
export const getCommentsByMovieId = async (movieId: number) => {
    return axios.get(`${API_BASE_URL}/getallitems`, {
        params: {
            typeIndex: 4,
            movieId
        }
    })
}